<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="utf-8" lang="utf-8">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>
   数据库例子代码 - CodeIgniter 中文手册|用户手册|用户指南|Wiki文档
  </title>
  <link rel="shortcut icon" href="/user_guide/../images/design/favicon.ico" type="image/x-icon" />
  <link rel="stylesheet" type="text/css" media="all" href="/user_guide/userguide.css" />
  <link rel="search" href="/user_guide/../CodeIgniterSearch.xml" type="application/opensearchdescription+xml" title="CodeIgniter 搜索" />
  <link rel="canonical" href="/user_guide/database/examples.html" />
  <script type="text/javascript" src="/user_guide/nav/nav.js">
  </script>
  <script type="text/javascript" src="/user_guide/nav/prototype.lite.js">
  </script>
  <script type="text/javascript" src="/user_guide/nav/moo.fx.js">
  </script>
  <script type="text/javascript" src="/user_guide/nav/user_guide_menu.js">
  </script>
  <meta name="robots" content="all" />
  <meta name="author" content="ExpressionEngine Dev Team" />
  <meta name="description" content="CodeIgniter 中文手册, CodeIgniter 用户指南, CodeIgniter User Guide, Wiki 文档" />
 </head>
 <body>
  <!-- START NAVIGATION -->
  <div id="nav">
   <div id="nav_inner">
    <script type="text/javascript">
     create_menu('/user_guide/');
    </script>
   </div>
  </div>
  <script type="text/javascript">
   _setNavigation();
  </script>
  <div id="nav2">
   <a name="top">
   </a>
   <a href="javascript:void(0);" onclick="myHeight.toggle();">
    <img src="/user_guide/images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="切换目录" alt="切换目录" />
   </a>
  </div>
  <div id="masthead">
   <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
    <tr>
     <td width="350">
      <h1>
       CodeIgniter 用户指南 版本 2.0.0
      </h1>
     </td>
     
     <td id="breadcrumb_right">
      <a href="/user_guide/toc.html">
       目录页
      </a>
     </td>
    </tr>
   </table>
  </div>
  <!-- END NAVIGATION -->
  <!-- START BREADCRUMB -->
  <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
   <tr>
    <td id="breadcrumb">
     <a href="/" target="_blank">
      CodeIgniter 中国首页
     </a>
     &nbsp;&#8250;&nbsp;
     <a href="/user_guide/toc.html">用户指南目录</a>
     &nbsp;&#8250;&nbsp;
     <a href="/user_guide/database/index.html">
      数据库类
     </a>
     &nbsp;&#8250;&nbsp;数据库例子代码
    </td>
    <td id="searchbox">
     <form method="get" action="http://www.google.com/search" target="google_window">
      <input type="hidden" name="client" value="pub-0176846097796333" />
      <input type="hidden" name="forid" value="1" />
      <input type="hidden" name="ie" value="UTF-8" />
      <input type="hidden" name="oe" value="UTF-8" />
      <input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.org.cn/user_guide/" />
      搜索用户指南&nbsp;
      <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />
      &nbsp;
      <input type="submit" class="submit" name="sa" value="Go" />
     </form>
    </td>
   </tr>
  </table>
  <!-- END BREADCRUMB -->
  <div style="clear:both;text-align:right;padding: 6px 40px 0 0;">
   <a href="http://codeigniter.com/user_guide/database/examples.html" target="_blank">
    查看原文
   </a>
  </div>
  <!--<br clear="all"  />-->
  <!-- START CONTENT -->
  <div id="content">
   <h1>
    数据库快速入门例子代码
   </h1>
   <p>
    下面的内容将简单说明怎样使用数据库。更详细的信息请阅读各个函数的单独介绍页面。
   </p>
   <h2>
    初始化数据库类
   </h2>
   <p>
    下面的代码将依据你的
    <a href="configuration.html">
     数据库配置
    </a>
    载入并初始化数据库类:
   </p>
   <code>
    $this->load->database();
   </code>
   <p>
    一旦被载入，你可以在任何地方像这样使用它：
   </p>
   <p>
    注意: 如果你的所有页面均要求初始化数据库类，你可以让它自动加载。详见
    <a href="connecting.html">
     数据库连接
    </a>
    。
   </p>
   <h2>
    多结果标准查询（对象形式）
   </h2>
   <code>
    $query = $this->db->query('SELECT name, title, email FROM my_table');
    <br />
    <br />
    foreach ($query->result() as $row)
    <br />
    &#123;
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;echo $row->title;
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;echo $row->name;
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;echo $row->email;
    <br />
    &#125;
    <br />
    <br />
    echo 'Total Results: ' . $query->num_rows();
   </code>
   <p>
    上面的
    <dfn>
     result()
    </dfn>
    函数返回一个
    <strong>
     对象
    </strong>
    的数组。例如：$row->title
   </p>
   <h2>
    多结果标准查询（数组形式）
   </h2>
   <code>
    $query = $this->db->query('SELECT name, title, email FROM my_table');
    <br />
    <br />
    foreach ($query->result_array() as $row)
    <br />
    &#123;
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;echo $row['title'];
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;echo $row['name'];
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;echo $row['email'];
    <br />
    &#125;
   </code>
   <p>
    上面的
    <dfn>
     result_array()
    </dfn>
    函数返回一个带下标的数组。例如：$row['title']
   </p>
   <h2>
    测试查询结果
   </h2>
   <p>
    如果你的查询可能不返回结果，我们建议你先使用
    <dfn>
     num_rows()
    </dfn>
    函数来测试:
   </p>
   <code>
    $query = $this->db->query("YOUR QUERY");
    <br />
    <br />
    if ($query->num_rows() > 0)
    <br />
    &#123;
    <br />
    &nbsp;&nbsp;&nbsp;foreach ($query->result() as $row)
    <br />
    &nbsp;&nbsp;&nbsp;&#123;
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $row->title;
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $row->name;
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $row->body;
    <br />
    &nbsp;&nbsp;&nbsp;&#125;
    <br />
    &#125;
   </code>
   <h2>
    单结果标准查询（对象形式）
   </h2>
   <code>
    $query = $this->db->query('SELECT name FROM my_table LIMIT 1');
    <br />
    <br />
    $row = $query->row();
    <br />
    echo $row->name;
    <br />
   </code>
   <p>
    上面的
    <dfn>
     row()
    </dfn>
    函数返回一个
    <strong>
     对象
    </strong>
    。例如：$row->name
   </p>
   <h2>
    单结果标准查询（数组形式）
   </h2>
   <code>
    $query = $this->db->query('SELECT name FROM my_table LIMIT 1');
    <br />
    <br />
    $row = $query->row_array();
    <br />
    echo $row['name'];
    <br />
   </code>
   <p>
    上面的
    <dfn>
     row_array()
    </dfn>
    函数返回一个
    <strong>
     数组
    </strong>
    。例如：$row['name']
   </p>
   <h2>
    标准插入(insert)
   </h2>
   <code>
    $sql = "INSERT INTO mytable (title, name)
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VALUES (".$this->db->escape($title).", ".$this->db->escape($name).")";
    <br />
    <br />
    $this->db->query($sql);
    <br />
    <br />
    echo $this->db->affected_rows();
   </code>
   <h2>
    快捷查询
   </h2>
   <p>
    <a href="active_record.html">
     快捷查询类
    </a>
    能为我们提供快速取得数据的途径:
   </p>
   <code>
    $query = $this->db->get('table_name');
    <br />
    <br />
    foreach ($query->result() as $row)
    <br />
    &#123;
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;echo $row->title;
    <br />
    &#125;
   </code>
   <p>
    上面的
    <dfn>
     get()
    </dfn>
    函数返回数据表中所有的结果。
    <a href="active_record.html">
     快捷查询类
    </a>
    提供所有数据库操作的快捷函数。
   </p>
   <h2>
    快捷插入(insert)
   </h2>
   <code>
    $data = array(
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'title' => $title,
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'name' => $name,
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'date' => $date
    <br />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);
    <br />
    <br />
    $this->db->insert('mytable', $data);
    <br />
    <br />
    // Produces: INSERT INTO mytable (title, name, date) VALUES ('&#123;$title&#125;', '&#123;$name&#125;', '&#123;$date&#125;')
   </code>
   <p>
    &nbsp;
   </p>
   <div id="Contributors">
    翻译贡献者:
gaochao, Hex, longbill
   </div>
   <div id="DocDate">
    最后修改: 2008-01-02 20:21:50
   </div>
  </div>
  <!-- END CONTENT -->
  <div id="footer">
   <p>
    上一个主题:&nbsp;&nbsp;
    <a href="/user_guide/database/index.html">
     数据库类
    </a>
    &nbsp;&nbsp;&middot;&nbsp;&nbsp;
    <a href="#top">
     页首
    </a>
    &nbsp;&middot;&nbsp;&nbsp;
    <a href="/user_guide/toc.html">用户指南目录</a>
    &nbsp;&nbsp;&middot;&nbsp;&nbsp;下一个主题:&nbsp;&nbsp;
    <a href="/user_guide/database/configuration.html">
     数据库配置
    </a>
   </p>
   <p>
    <a href="http://codeigniter.com">
     CodeIgniter
    </a>
    &nbsp;&middot;&nbsp; 版权所有 &#169; 2006-2011 &nbsp;&middot;&nbsp;
    <a href="http://ellislab.com/">
     Ellislab, Inc.
    </a>
   </p>
   <p>
    中文化:
    <a href="">
     CodeIgniter 中国
    </a>
    &nbsp;&middot;&nbsp; 制作: Hex &nbsp;&middot;&nbsp; 版本: 1.20 &nbsp;&middot;&nbsp; 鸣谢: 子非鱼
   </p>
  </div>
  
  <div style="display:none">
   
   <noscript>
    <a href="http://www.51.la/?3289908" target="_blank">
     <img alt="我要啦免费统计" src="http://img.users.51.la/3289908.asp" style="border:none" />
    </a>
   </noscript>
  </div>
 </body>
</html>